package org.andao.commons.taglib.ui.tree;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.andao.core.base.model.TreeSupportModel;
import org.andao.core.utils.ExStringUtils;


/**
 * 
 * 构建数据树.<p>
 * 
 * @author Marco.hu(hzg139@163.com)
 */
public class TreeBuilder {

	/**
	 * 构造数据树 (NodeId为resourceid)
	 * @param treeList 
	 * @param rootNodeCode
	 * @param checkedValues
	 * @return
	 */
	public static ZTreeNode buildTreeById(List<? extends TreeSupportModel> treeList,String rootNodeCode,String checkedValues){
		ZTreeNode zTreeNode = null;		
		ZTreeNode zTreeNode1 = null;	
		
		Map<String,ZTreeNode> map = new HashMap<String, ZTreeNode>();
		boolean checked = false;
//		for(TreeSupportModel treeNode:treeList){	
//			if(ExStringUtils.isNotEmpty(checkedValues) && checkedValues.indexOf(treeNode.getNodeId()) >=0){
//				checked = true;
//			}else{
//				checked = false;
//			}
//			if(treeNode.getNodeCode().equals(rootNodeCode)){				
//				zTreeNode =  new ZTreeNode(treeNode.getNodeName(),treeNode.getNodeId(),treeNode.getParentNodeId(),true,checked,treeNode.getNodeLevel(),new LinkedHashSet<ZTreeNode>());
//				map.put(treeNode.getNodeId(), zTreeNode);
//			}else {			
//				if(null!=map.get(treeNode.getParentNodeId())){
//					ZTreeNode parentOrg = map.get(treeNode.getParentNodeId());
//					zTreeNode1 =  new ZTreeNode(treeNode.getNodeName(),treeNode.getNodeId(),treeNode.getParentNodeId(),true,checked,treeNode.getNodeLevel(),new LinkedHashSet<ZTreeNode>());
//					map.put(treeNode.getNodeId(), zTreeNode1);
//					parentOrg.getNodes().add(zTreeNode1);				
//				}
//			}	
//		}
		//Charles modify 8/29
		int checkCount = -1;//已经检验过的个数
		TreeSupportModel treeNode;
		while(checkCount++ <treeList.size()){
			for (int i = 0; i < treeList.size();i++) {
				treeNode = treeList.get(i);
				if(null==map.get(treeNode.getNodeId())){
					if(ExStringUtils.isNotEmpty(checkedValues) && checkedValues.indexOf(treeNode.getNodeId()) >=0){
						checked = true;
					}else{
						checked = false;
					}
					if(treeNode.getNodeCode().equals(rootNodeCode)){				
						zTreeNode =  new ZTreeNode(treeNode.getNodeName(),treeNode.getNodeId(),treeNode.getParentNodeId(),true,checked,treeNode.getNodeLevel(),new LinkedHashSet<ZTreeNode>());
						map.put(treeNode.getNodeId(), zTreeNode);
					}else {			
						if(null!=map.get(treeNode.getParentNodeId())){
							ZTreeNode parentOrg = map.get(treeNode.getParentNodeId());
							zTreeNode1 =  new ZTreeNode(treeNode.getNodeName(),treeNode.getNodeId(),treeNode.getParentNodeId(),true,checked,treeNode.getNodeLevel(),new LinkedHashSet<ZTreeNode>());
							map.put(treeNode.getNodeId(), zTreeNode1);
							parentOrg.getNodes().add(zTreeNode1);				
						}
					}	
				}
			}
		}
		map = null;
		zTreeNode1 = null;
		return zTreeNode;
	}
	
	/**
	 * 构造数据树(NodeId 为编码)
	 * @param treeList
	 * @param rootNodeCode
	 * @param checkedCodes
	 * @return
	 */
	public static ZTreeNode buildTreeByCode(List<? extends TreeSupportModel> treeList,String rootNodeCode,String checkedCodes){
		ZTreeNode zTreeNode = null;		
		ZTreeNode zTreeNode1 = null;	
		
		Map<String,ZTreeNode> map = new HashMap<String, ZTreeNode>();
		boolean checked = false;
		for(TreeSupportModel treeNode : treeList){	
			if(ExStringUtils.isNotEmpty(checkedCodes) && checkedCodes.indexOf(treeNode.getNodeCode()) >=0){
				checked = true;
			}else{
				checked = false;
			}
			if(treeNode.getNodeCode().equals(rootNodeCode)){				
				zTreeNode =  new ZTreeNode(treeNode.getNodeName(),treeNode.getNodeCode(),treeNode.getParentNodeId(),true,checked,treeNode.getNodeLevel(),new LinkedHashSet<ZTreeNode>());
				map.put(treeNode.getNodeId(), zTreeNode);
			}else {
				if(null!=map.get(treeNode.getParentNodeId())){
					ZTreeNode parentOrg = map.get(treeNode.getParentNodeId());
					zTreeNode1 =  new ZTreeNode(treeNode.getNodeName(),treeNode.getNodeCode(),treeNode.getParentNodeId(),true,checked,treeNode.getNodeLevel(),new LinkedHashSet<ZTreeNode>());
					map.put(treeNode.getNodeId(), zTreeNode1);
					parentOrg.getNodes().add(zTreeNode1);				
				}
			}	
		}
		map = null;
		zTreeNode1 = null;
		return zTreeNode;
	}
	
	/**
	 * 获取树形结构节点列表
	 * 
	 * @param treeList
	 * @param rootNodeCode
	 * @param checkedValues
	 * @return
	 * @author Seewo Software - Vick.liu (liuguanglin@cvte.cn)
	 */
	public static List<ZTreeNode> getTreeNodeList(List<? extends TreeSupportModel> treeList,String rootNodeCode,String checkedValues){
		return getTreeNodeList(buildTreeById(treeList, rootNodeCode, checkedValues));
	}
	
	/**
	 * 获取树形结构节点列表
	 * 
	 * @param rootNode
	 * @return
	 * @author Seewo Software - Vick.liu (liuguanglin@cvte.cn)
	 */
	public static List<ZTreeNode> getTreeNodeList(ZTreeNode rootNode){
		List<ZTreeNode> list = new ArrayList<ZTreeNode>();
		if(null!=rootNode){
			addTreeNodeToList(rootNode,list);
		}
		return list;
	}
	

	//递归将 treeNode 添加到List中
	private static void addTreeNodeToList(ZTreeNode rootNode,List<ZTreeNode> list){
		list.add(rootNode);
		Set<ZTreeNode> childNodes = rootNode.getNodes();
		if(null == childNodes || 0 == childNodes.size()){
			return;
		}
		for (ZTreeNode zTreeNode : childNodes) {
			addTreeNodeToList(zTreeNode,list);
		}
	}
	
	
	/**
	 * 获取树形结构节点列表
	 * 
	 * @param treeList
	 * @param rootNodeCode
	 * @param checkedValues
	 * @return
	 * @author Seewo Software - Vick.liu (liuguanglin@cvte.cn)
	 */
	public static List<String> getTreeIdList(List<? extends TreeSupportModel> treeList,String rootNodeCode,String checkedValues){
		return getTreeIdList(buildTreeById(treeList, rootNodeCode, checkedValues));
	}
	
	/**
	 * 获取树形结构节点列表
	 * 
	 * @param rootNode
	 * @return
	 * @author Seewo Software - Vick.liu (liuguanglin@cvte.cn)
	 */
	public static List<String> getTreeIdList(ZTreeNode rootNode){
		List<String> list = new ArrayList<String>();
		if(null!=rootNode){
			addTreeIdToList(rootNode,list);
		}
		return list;
	}
	

	//递归将 treeNode 添加到List中
	private static void addTreeIdToList(ZTreeNode rootNode,List<String> list){
		list.add(rootNode.getId());
		Set<ZTreeNode> childNodes = rootNode.getNodes();
		if(null == childNodes || 0 == childNodes.size()){
			return;
		}
		for (ZTreeNode zTreeNode : childNodes) {
			addTreeIdToList(zTreeNode,list);
		}
	}
	
}
